import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QFileDialog,QWidget
from qt_02openFile_ui import Ui_MainWindow

class MainWindow(QMainWindow, Ui_MainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.setupUi(self)
        self.pushButton.clicked.connect(self.open_file_dialog)

    def open_file_dialog(self):
        # 打开文件对话框，获取文件路径
        file_path, _ = QFileDialog.getOpenFileName(self, "选择文件", "", "All Files (*);;Text Files (*.txt)")
        if file_path:
            # 如果获取到文件路径，则将其显示在文本框中
            self.lineEdit.setText(file_path)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWindow() # 创建主窗口对象
    window.show() #   显示主窗口
    sys.exit(app.exec_())